#include "Ball.h"

Ball::Ball():radius(1.0f), slices(10), stack(10), pos(D3DXVECTOR3(0.0f, 0.0f, 0.0f))
{
	//create the ball 
	createBall(this->radius, this->slices, this->stack);
	accumTime = 0.0f;
	
//NOTE -- Ay is the acceleration in the Y-direction (up and down).
//Students will need to fill in this value.
////////////////////////////////////////////////////////////////
	Ay = -9.8;
////////////////////////////////////////////////////////////////
	lastVy = 0;
}

Ball::~Ball()
{
	ReleaseCOM(ballMesh);
	delete ballMesh;

}
Ball::Ball(float radius, unsigned int slices, unsigned int stack, float x, float y, float z)
{
	//set the ball params
	this->radius = radius;
	this->slices = slices;
	this->stack = stack;

	pos = D3DXVECTOR3(x, y, z);

	//create the ball 
	createBall(this->radius, this->slices, this->stack);
	accumTime = 0.0f;

	Ay = -9.8;
	lastVy = 0;


}


void Ball::createBall(float radius, unsigned int slices, unsigned int stack)
{
	//provide the radius, slices, and stack, the mesh we have, null for adjacency
    D3DXCreateSphere(gd3dDevice, radius, slices, stack, &ballMesh, NULL);
}  
 
void Ball::draw()
{
  HR(gd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME));
  HR(ballMesh->DrawSubset(0));
}

//NOTE -- this function updates the ball's position in the Y-direction(up and down)
//Students will need to fill in the entire function with the exception of anything included in the if statement
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Ball::update(float dt)
{
	//This should be done by the students.
	///////////////////////////////////////////////////
	pos.y += (lastVy * dt) + (.5 * Ay * dt * dt);
	lastVy += Ay * dt;
	///////////////////////////////////////////////////
	
	//This can remain in the code
	/////////////////////////////
	if(pos.y < 0)
	{
		//t = 0;
		Ay = lastVy = 0;
	}
	else
		accumTime += dt;
	////////////////////////////
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////